package views
{
	import events.ProjectEventViewEvent;
	import events.ProjectorBoardEvent;
	
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	import gzgi.text.TimeFormater;
	import gzgi.utils.TimeUtil;
	
	import module.ProjectEventModule;
	
	import mx.controls.Label;
	import mx.core.IUIComponent;
	import mx.core.IVisualElementContainer;
	import mx.graphics.IFill;
	import mx.graphics.IStroke;
	import mx.graphics.SolidColor;
	import mx.graphics.SolidColorStroke;
	
	import spark.components.RichEditableText;
	import spark.components.SkinnableContainer;
	import spark.core.IGraphicElement;
	import spark.primitives.Rect;
	
	public class ProjectEventView extends SkinnableContainer implements IProjectEventView
	{
		private var m_source:ProjectEventModule;
		private var m_rectLayer:SkinnableContainer;
		private var m_labelLayer:SkinnableContainer;
		private var m_edgeLayer:SkinnableContainer;
		private var m_timeLabelLayer:SkinnableContainer;
		private var m_labelList:Vector.<Label>;
		private var m_selectedFill:IFill;
		private var m_normalFill:IFill;
		private var m_color:Number;
		private var m_strok:IStroke;
		private var m_selected:Boolean;
		private var m_currentRectangleList:Vector.<Rectangle>;
		private var m_editableTitleText:RichEditableText;
		
		private var m_topEdgeBar:BlankViews;
		private var m_bottomEdgeBar:BlankViews;
		
		
		private static const REC_RADIUS:Number = 5;
		private static const EDGE_HEIGHT:Number = 5;
		private static const HEIGHT_TIMETEXT:Number = 12;
		static private const LINE_FIXED:Number = 1;
		public function ProjectEventView()
		{
			super();
			this.doubleClickEnabled = true;
			m_rectLayer = new SkinnableContainer();
			m_labelLayer = new SkinnableContainer();
			m_edgeLayer = new SkinnableContainer();
			m_timeLabelLayer = new SkinnableContainer();
			m_labelList = new Vector.<Label>();
			m_selected = false;
			
			addElement(m_rectLayer);
			addElement(m_labelLayer);
			addElement(m_timeLabelLayer);
			addElement(m_edgeLayer);
			
			m_color = 0xcc0000;
			m_selectedFill = new SolidColor(m_color, 0.7);
			m_normalFill = new SolidColor(m_color, 0.3);
			m_strok = new SolidColorStroke(m_color, 1, 0.8);
			
		}
		public function setSource($source:ProjectEventModule):void
		{
			m_source = $source;
		}
		public function set enableShotEditting($b:Boolean):void
		{
			if ($b)
			{
				if (m_currentRectangleList == null) return;
				var rectangle:Rectangle = m_currentRectangleList[0];
				if (rectangle.height > HEIGHT_TIMETEXT)
				{
					var firstLabel:Label = this.m_labelList[0];
					m_editableTitleText = new RichEditableText();
					m_editableTitleText.width = rectangle.width;
					m_editableTitleText.x = rectangle.x;
					m_editableTitleText.y = firstLabel.y;
					m_editableTitleText.height = rectangle.y + rectangle.height - m_editableTitleText.y;
					m_editableTitleText.text = firstLabel.text;
					m_editableTitleText.setStyle("color", GlobalUtil.PROJECTVIEW_FONT_SELECTED_COLOR);
					this.m_labelLayer.addElement(m_editableTitleText);
					m_editableTitleText.focusManager.setFocus(m_editableTitleText);
					firstLabel.visible = false;
				}
				
			}
			else
			{
				if (m_editableTitleText != null)
				{
					this.m_labelLayer.removeElement(m_editableTitleText);
					var firstLabel:Label = this.m_labelList[0];
					firstLabel.visible = true;
					for (var i:Number = 0; i < this.m_labelList.length; i++)
					{
						var label:Label = this.m_labelList[i] as Label;
						label.text = m_editableTitleText.text;
					}
					this.source._title(firstLabel.text);
					m_editableTitleText = null;
				}
				
			}
		}
		public function set selected($b:Boolean):void
		{
			var useFill:IFill = $b ? m_selectedFill : m_normalFill;
			var useColor:Number = $b ? GlobalUtil.PROJECTVIEW_FONT_SELECTED_COLOR : this.m_color;
			m_selected = $b;
			for (var i:Number = 0; i < this.m_rectLayer.numElements; i++)
			{
				var rect:Rect = this.m_rectLayer.getElementAt(i) as Rect;
//				trace (rect);
				rect.fill = useFill;
				rect.stroke = m_strok;
			}
			for (var i:Number = 0; i < this.m_labelList.length; i++)
			{
				var label:Label = this.m_labelList[i] as Label;
				label.setStyle("color", useColor);
			}
			for (var i:Number = 0; i < this.m_timeLabelLayer.numElements; i++)
			{
				var label:Label = this.m_timeLabelLayer.getElementAt(i) as Label;
				label.setStyle("color", useColor);
			}
		}
		public function get selected():Boolean
		{
			return m_selected;
		}
		public function updateGraphicWithEventModule($eventModule:ProjectEventModule, $container:RowProjectBoard):void
		{

			var vector:Vector.<Rectangle> = this.formRectList($eventModule.startTime, $eventModule.endTime, $container);
			m_currentRectangleList = vector;
			m_rectLayer.removeAllElements();
			m_timeLabelLayer.removeAllElements();
			//create labels
			if (vector.length != m_labelList.length)
			{
				m_labelLayer.removeAllElements();
				m_labelList = new Vector.<Label>();
				for (var i:Number = 0; i < vector.length; i++)
				{
					var rectangle:Rectangle = vector[i] as Rectangle;
					var newLabel:Label = new Label();
					newLabel.width = rectangle.width;
					newLabel.x = rectangle.x;
					newLabel.y = rectangle.y;
//					label.height = rectangle.height;
					newLabel.text = m_source.title;
//					label.visible = false;
					m_labelList.push(newLabel);
					m_labelLayer.addElement(newLabel);
				}
			}
			
			//draw edge bar
			for (var i:Number = 0; i < vector.length; i++)
			{
				var rectangle:Rectangle = vector[i] as Rectangle;
				if (i == 0)
				{
					if (m_topEdgeBar == null)
					{
						m_topEdgeBar = new BlankViews(rectangle.width, EDGE_HEIGHT);
						m_edgeLayer.addElement(m_topEdgeBar);
						m_topEdgeBar.addEventListener( MouseEvent.MOUSE_DOWN, handleEdgeSelect, false, 0, true);
						m_topEdgeBar.addEventListener( MouseEvent.MOUSE_OVER, handleEdgeOver, false, 0, true);
					}
					m_topEdgeBar.x = rectangle.x;
					m_topEdgeBar.y = rectangle.y;
				}
				if (i == vector.length - 1)
				{
					if (m_bottomEdgeBar == null)
					{
						m_bottomEdgeBar = new BlankViews(rectangle.width, EDGE_HEIGHT);
						m_edgeLayer.addElement(m_bottomEdgeBar);
						m_bottomEdgeBar.addEventListener( MouseEvent.MOUSE_DOWN, handleEdgeSelect, false, 0, true);
						m_bottomEdgeBar.addEventListener( MouseEvent.MOUSE_OVER, handleEdgeOver, false, 0, true);
					}
					m_bottomEdgeBar.x = rectangle.x;
					m_bottomEdgeBar.y = rectangle.y + rectangle.height - EDGE_HEIGHT;
				}
			}
			
			
			//draw rects
			for (var i:Number = 0; i < vector.length; i++)
			{
				var rectangle:Rectangle = vector[i] as Rectangle;
				var rect:Rect = new Rect();
				rect.x = rectangle.x;
				rect.y = rectangle.y;
				rect.width = rectangle.width;
				rect.height = rectangle.height;
				
				if (i == 0)
				{
					rect.topLeftRadiusX = rect.topLeftRadiusY = rect.topRightRadiusX = rect.topRightRadiusY = REC_RADIUS;
//					var label:Label = m_labelLayer.getElementAt(i) as Label;
					var label:Label = this.m_labelList[i] as Label;
					if (rectangle.height > HEIGHT_TIMETEXT && rectangle.height < 3 * HEIGHT_TIMETEXT)
					{
						label.visible = true;
						label.x = rectangle.x;
						label.y = rectangle.y;
					}
					else if (rectangle.height > HEIGHT_TIMETEXT * 3)
					{
						
						label.visible = true;
						label.x = rectangle.x;
						label.y = rectangle.y + HEIGHT_TIMETEXT;
						
					}
					else
					{
						label.visible = false;
					}
				}
				if (i == vector.length - 1)
				{
					rect.bottomLeftRadiusX = rect.bottomLeftRadiusY = rect.bottomRightRadiusX = rect.bottomRightRadiusY = REC_RADIUS;
					
				}
				m_rectLayer.addElement(rect);

			}
			
			// time label
			for (var i:Number = 0; i < vector.length; i++)
			{
				var rectangle:Rectangle = vector[i] as Rectangle;
				var timeFormater:TimeFormater = new TimeFormater("hh:mm");
				if (i == 0)
				{
					var timeLabel:Label = new Label();
					timeLabel.visible = false;
					timeLabel.width = rectangle.width;
					timeLabel.x = rectangle.x;
					timeLabel.y = rectangle.y;
					timeLabel.text = timeFormater.format($eventModule.startTime);
					this.m_timeLabelLayer.addElement(timeLabel);
					if (rectangle.height > HEIGHT_TIMETEXT * 3)
					{
						timeLabel.visible = true;
					}
				}
				if (i == vector.length - 1)
				{
					var timeLabel:Label = new Label();
					timeLabel.visible = false;
					timeLabel.width = rectangle.width;
					timeLabel.x = rectangle.x;
					timeLabel.y = rectangle.y + rectangle.height - HEIGHT_TIMETEXT;
					timeLabel.text = timeFormater.format($eventModule.endTime);
					this.m_timeLabelLayer.addElement(timeLabel);
					if (rectangle.height > HEIGHT_TIMETEXT * 3)
					{
						timeLabel.visible = true
					}
				}
				
			}
			this.selected = this.selected;
		}
		private function formRectList($startDate:Date, $endDate:Date, $board:RowProjectBoard):Vector.<Rectangle>
		{
			var targetDayIndex:Number = $board.type == RowProjectBoard.TYPE_WEEK 
										? TimeUtil.getDateIndexFrom1970(TimeUtil.getFirstDateOfWeek($board.date))
										: TimeUtil.getDateIndexFrom1970($board.date);
			var startIndex:Number = TimeUtil.getDateIndexFrom1970(TimeUtil.getFirstDateOfWeek($startDate)) + $startDate.getDay();
			var endIndex:Number = TimeUtil.getDateIndexFrom1970(TimeUtil.getFirstDateOfWeek($endDate)) + $endDate.getDay();
			
			startIndex -= targetDayIndex;
			endIndex -= targetDayIndex;
			
			var columnWidth:Number = $board.gridRect.width / $board.columns;
			var vector:Vector.<Rectangle> = new Vector.<Rectangle>();
			for (var i:Number = startIndex; i <= endIndex; i++)
			{
				var rectangle:Rectangle = new Rectangle();
				rectangle.x = $board.gridRect.x + i * columnWidth;
				rectangle.width = columnWidth - LINE_FIXED;
				if (i == startIndex)
				{
					rectangle.y = $board.gridRect.y + RowProjectBoard.timeToPixels($startDate);
				}
				else
				{
					rectangle.y = $board.gridRect.y;
				}
				if (i == endIndex)
				{
					rectangle.height = $board.gridRect.y + RowProjectBoard.timeToPixels($endDate) - rectangle.y;
				}
				else
				{
					rectangle.height = $board.gridRect.y + $board.gridRect.height - rectangle.y;
				}
				vector.push(rectangle);
			}
			return vector;
		}
		private function handleEdgeSelect($event:MouseEvent):void
		{
			var event:ProjectEventViewEvent = new ProjectEventViewEvent($event.currentTarget == this.m_topEdgeBar ? ProjectEventViewEvent.TOP_EDGE_SELECT : ProjectEventViewEvent.BOTTOM_EDGE_SELECT, true, true);
			$event.currentTarget.dispatchEvent(event);
		}
		private function handleEdgeOver($event:MouseEvent):void
		{
			var event:ProjectEventViewEvent = new ProjectEventViewEvent($event.currentTarget == this.m_topEdgeBar ? ProjectEventViewEvent.TOP_EDGE_OVER : ProjectEventViewEvent.BOTTOM_EDGE_OVER, true, true);
			$event.currentTarget.dispatchEvent(event);
		}
		public function removeFromParentView():void
		{
			if (this.parent != null)
			{
				IVisualElementContainer(this.parent).removeElement(this);
			}
		}
		public function get source():ProjectEventModule
		{
			return m_source;
		}
		
	}
}